package com.x.provider.oss.controller.app;

import cn.hutool.core.io.FileUtil;
import com.x.core.utils.BeanUtil;
import com.x.core.web.api.R;
import com.x.core.web.controller.BaseFrontendController;
import com.x.provider.api.oss.model.dto.green.GreenRequestDTO;
import com.x.provider.oss.model.bo.GreenContentRequestBO;
import com.x.provider.oss.model.vo.oss.GreenReqVO;
import com.x.provider.oss.model.vo.oss.TencentOssCredentialVO;
import com.x.provider.oss.service.ContentGreenService;
import com.x.provider.oss.service.GreenService;
import com.x.provider.oss.service.OssService;
import com.x.provider.oss.service.TencentOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

@Api(tags = "对象存储服务")
@RestController
@RequestMapping("/app/oss")
public class OssController extends BaseFrontendController {

    private final OssService ossService;
    private final TencentOssService tencentOssService;
    private final GreenService greenService;

    public OssController(OssService ossService,
                         TencentOssService tencentOssService,
                         GreenService greenService){
        this.ossService = ossService;
        this.tencentOssService = tencentOssService;
        this.greenService = greenService;
    }

    @ApiOperation(value = "获取对象访问url")
    @GetMapping("/browser/url")
    public R<String> getObjectBrowseUrl(@RequestParam @ApiParam(value = "对象key") String objectKey){
        return R.ok(ossService.getObjectBrowseUrl(objectKey));
    }

    @ApiOperation(value = "获取对象上传参数")
    @GetMapping("/tencent/upload/credential")
    public R<TencentOssCredentialVO> getTencentOssUploadCredentia(@RequestParam @NotBlank @ApiParam(value = "上传文件名") String fileName){
        return R.ok(tencentOssService.getTencentOssUploadCredentia(getCurrentCustomerId(), FileUtil.extName(fileName)));
    }

    @ApiOperation(value = "内容安全审核，可对图片，视频进行安全审核， 返回结果 1 通过 其余值都是拒绝")
    @GetMapping("/green/sync")
    public R<Integer> getObjectBrowseUrl(@RequestBody GreenReqVO reqVO){
        return R.ok(greenService.green(BeanUtil.prepare(reqVO, GreenRequestDTO.class)).getValue());
    }

}
